package com.lab.system.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.lab.common.core.domain.model.LoginUser;
import com.lab.common.enums.LabStatus;
import com.lab.system.domain.Equipment;
import com.lab.system.domain.Lab;
import com.lab.system.mapper.EquipmentMapper;
import com.lab.system.mapper.LabMapper;
import com.lab.system.service.IEquipmentService;
import com.lab.system.service.ILabService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 实验设备Service业务层处理
 *
 * @author cf
 * @date 2025-02-09
 */
@Service
public class EquipmentServiceImpl implements IEquipmentService {
    @Autowired
    private EquipmentMapper equipmentMapper;

    @Autowired
    private LabMapper labMapper;

    @Autowired
    private ILabService labService;

    /**
     * 查询实验设备
     *
     * @param id 实验设备主键
     * @return 实验设备
     */
    @Override
    public Equipment selectEquipmentById(Long id) {
        return equipmentMapper.selectEquipmentById(id);
    }

    /**
     * 查询实验设备列表
     *
     * @param equipment 实验设备
     * @return 实验设备
     */
    @Override
    public List<Equipment> selectEquipmentList(Equipment equipment, LoginUser loginUser) {
        if (loginUser != null) {
            // 数据权限控制
            List<Lab> labs = labService.selectLabList(new Lab(),loginUser);
            List<Long> labIds = labs.stream().map(Lab::getId).collect(Collectors.toList());
            equipment.setLabIds(labIds);
        }

        List<Equipment> equipmentList = equipmentMapper.selectEquipmentList(equipment);
        List<Long> labIds = equipmentList.stream().map(Equipment::getLabId).collect(Collectors.toList());

        Lab lab = new Lab();
        lab.setLabIds(labIds);
        // 查询所有的实验室
        List<Lab> labs = labMapper.selectLabList(lab);
        Map<Long,String> labMap = labs.stream().collect(Collectors.toMap(Lab::getId, Lab::getLabName));
        equipmentList.forEach(x->{
            x.setLabName(labMap.get(x.getLabId()));
            x.setStatusDesc(Objects.requireNonNull(LabStatus.getLabStatus(x.getStatus())).getInfo());
        });

        return equipmentList;
    }

    /**
     * 新增实验设备
     *
     * @param equipment 实验设备
     * @return 结果
     */
    @Override
    public int insertEquipment(Equipment equipment) {
            return equipmentMapper.insertEquipment(equipment);
    }

    /**
     * 修改实验设备
     *
     * @param equipment 实验设备
     * @return 结果
     */
    @Override
    public int updateEquipment(Equipment equipment) {
        return equipmentMapper.updateEquipment(equipment);
    }

    /**
     * 批量删除实验设备
     *
     * @param ids 需要删除的实验设备主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentByIds(Long[] ids) {
        return equipmentMapper.deleteEquipmentByIds(ids);
    }

    /**
     * 删除实验设备信息
     *
     * @param id 实验设备主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentById(Long id) {
        return equipmentMapper.deleteEquipmentById(id);
    }
}
